﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PublishData.DAL;
using PublishData.BLL;


namespace PublishData
{
    public partial class FormDoiMatKhau : Form
    {
        private User m_NguoiSuDung;
        public User NguoiSuDung
        {
            get { return m_NguoiSuDung; }
            private set { m_NguoiSuDung = value; }
        }
        public FormDoiMatKhau(User nguoiSuDung)
        {
            InitializeComponent();

            if (nguoiSuDung == null)
            {
                throw new Exception("Tham số nguoiSuDung == NULL!");
            }
            txtTenDangNhap.Text = nguoiSuDung.tenDangNhap;
            txtHoTen.Text = nguoiSuDung.hoTen;
            txtMatKhau.Focus();
            m_NguoiSuDung = nguoiSuDung;
        }

        private void OnDongYClick(object sender, EventArgs e)
        {
            string matKhauCu = txtMatKhau.Text;
            string matKhauMoi = txtMatKhauMoi.Text;
            string matKhauMoi2 = txtMatKhauMoi2.Text;
            string hoTen = txtHoTen.Text;
            if (hoTen.IsNullOrEmpty())
            {
                DialogUtils.ShowError("Bạn phải gõ họ tên!");
                txtHoTen.Focus();
                return;
            }
            if (matKhauCu != m_NguoiSuDung.matKhau)
            {
                DialogUtils.ShowError("Mật khẩu cũ không đúng!");
                txtMatKhau.Focus();
                return;
            }

            if (matKhauMoi != matKhauMoi2)
            {
                DialogUtils.ShowError("Bạn phải gõ hai mật khẩu mới giống nhau!");
                txtMatKhauMoi.Focus();
                return;
            }
            try
            {
                UserBLL.DoiMatKhau(m_NguoiSuDung, matKhauMoi, hoTen);
                m_NguoiSuDung.matKhau = matKhauMoi;
                m_NguoiSuDung.hoTen = txtHoTen.Text;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch
            {
                DialogUtils.ShowError("Không thể đổi mật khẩu được!");
            }
        }

        private void FormDoiMatKhau_Load(object sender, EventArgs e)
        {

        }
    }
}
